package com.glory.dgcpcpda.activity.cpc;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.mfg.DAWorkEndActivity;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class CpcDaQtyUpdatedActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.vtLotId)
    FreeUI_ClearEditText vtLotId;
    @BindView(R.id.etEqpWorkID)
    FreeUI_ClearEditText etEqpWorkID;
    @BindView(R.id.vtRlfDoneQty)
    TextView vtRlfDoneQty;
    @BindView(R.id.tailLotDoneQty)
    TextView tailLotDoneQty;
    @BindView(R.id.vtRlfQty)
    FreeUI_ClearEditText vtRlfQty;
    @BindView(R.id.tailLotQty)
    FreeUI_ClearEditText tailLotQty;
    @BindView(R.id.isTailLot)
    TextView isTailLot;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    String IsBatch="N";
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_da_qty_updated;
    }

    @Override
    protected void initView() {
        initLayout();
    }
    private void initLayout() {
        tvTitle.setText("装片设备改数");
        setMainLayout(llMain);
        initEdit(etEqpWorkID,s->{

                FreeUI_EntityCallBack<BaseResponseBean<JsonObject>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<JsonObject>>
                        (new TypeToken<BaseResponseBean<JsonObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JsonObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            IsBatch=response.body().getResponse().getBody().get("IsBatch").toString().replace("\"","");//是否尾批
                            String BatchTotal=response.body().getResponse().getBody().get("BatchTotal").toString();//框架总条数
                            String BatchAccumulation=response.body().getResponse().getBody().get("BatchAccumulation").toString();//已生产条数
                            String Total=response.body().getResponse().getBody().get("Total").toString();//尾批总颗数
                            String Accumulation=response.body().getResponse().getBody().get("Accumulation").toString();//尾批已生产颗数
                            vtRlfQty.setText(BatchTotal);
                            vtRlfDoneQty.setText(BatchAccumulation);
                            tailLotQty.setText(Total);
                            tailLotDoneQty.setText(Accumulation);
                            String isBatchLot="";
                            if(StringUtils.isNotEmpty(IsBatch)){
                                isBatchLot=IsBatch.equals("Y")?"是":"否";
                            }
                            isTailLot.setText(isBatchLot);
                        }
                    }
                    @Override
                    public void onError(Response<BaseResponseBean<JsonObject>> response) {
                        showToast("获取设备数据失败，请检查设备是否联网",false);
                        super.onError(response);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("Action", "Query");
                map.addProperty("EqpID", getText(etEqpWorkID));
                map.addProperty("LotId", getText(vtLotId));
                //map.add("LOT", new JsonParser().parse(new Gson().toJson(new LotList(s))).getAsJsonObject());//传批次集合
                NetUtils.requestPostNet("", "EQPDaBondUnit", this, map, callback);

        });
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view){
        String count="";
        if("Y".equals(IsBatch)){//尾批取颗数
            count=getText(tailLotQty);
        }else{//整批取框架条数
            count=getText(vtRlfQty);
        }
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                FreeUI_EntityCallBack<BaseResponseBean<JsonObject>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<JsonObject>>
                        (new TypeToken<BaseResponseBean<JsonObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JsonObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                            showConfirmDialog(CpcDaQtyUpdatedActivity.this,"修改成功！");
                        }
                    }
                    @Override
                    public void onError(Response<BaseResponseBean<JsonObject>> response) {
                        showToast("修改失败，请检查设备是否联网",false);
                        super.onError(response);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("EqpID", getText(etEqpWorkID));
                map.addProperty("LotId", getText(vtLotId));
                map.addProperty("Action", "Set");
                map.addProperty("IsBatch", IsBatch);
                map.addProperty("Count", count);
                map.addProperty("Creator", Constant.CURRENT_USER);
                //map.add("LOT", new JsonParser().parse(new Gson().toJson(new LotList(s))).getAsJsonObject());//传批次集合
                NetUtils.requestPostNet("", "EQPDaBondUnit", this, map, callback);
                break;
        }
    }
    @Override
    protected void initData() {

    }
}
